<?php

/**
 * This is the model class for table "{{deal}}".
 *
 * The followings are the available columns in table '{{deal}}':
 * @property integer $id
 * @property integer $teamId
 * @property integer $createUserId
 * @property string $referenceNo
 * @property string $bank
 *
 * The followings are the available model relations:
 * @property Attachment[] $attachments
 * @property User $createUser
 * @property Team $team
 * @property DealCustomer[] $dealCustomers
 * @property Loan[] $loans
 */
class Deal extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Deal the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{deal}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('referenceNo, bank', 'required'),
			array('teamId, createUserId', 'numerical', 'integerOnly'=>true),
			array('referenceNo', 'length', 'max'=>100),
			array('bank', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, teamId, createUserId, referenceNo, bank', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'attachments' => array(self::HAS_MANY, 'Attachment', 'dealId'),
			'attachmentCount'=>array(self::STAT, 'Attachment', 'dealId',
				'condition'=>GlobalConfiguration::isManageMode()?'':'hidden<>1'),
			'createUser' => array(self::BELONGS_TO, 'User', 'createUserId'),
			'team' => array(self::BELONGS_TO, 'Team', 'teamId'),
			'dealCustomers' => array(self::HAS_MANY, 'DealCustomer', 'dealId'),
			'loans' => array(self::HAS_MANY, 'Loan', 'dealId'),
			'customers'=>array(self::HAS_MANY,'Customer','customerId','through'=>'dealCustomers'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'teamId' => 'Team',
			'createUserId' => 'Create User',
			'referenceNo' => 'File Reference No',
			'bank' => 'Bank',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		$criteria=new CDbCriteria;
		$criteria->compare('teamId',$this->teamId);
		$criteria->compare('createUserId',$this->createUserId);
		$criteria->compare('referenceNo',$this->referenceNo,true);
		$criteria->compare('bank',$this->bank,true);
		$criteria->order = 'referenceNo';
		
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public static function getBankArray() {
		return CHtml::listData(self::model()->findAll(array('select'=>'bank','distinct'=>true)), 'bank', 'bank');
	}
}